home *** CD-ROM | disk | FTP | other *** search
/ MacWorld 2003 August / MW 8 2003 CD1.iso / Inside Macworld / Product News / gimp-1.2.4.sit / gimp-1.2.4 / app / gimpbrushpipe.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-04-24  |  10.6 KB  |  414 lines

  1. /* The GIMP -- an image manipulation program
  2.  * Copyright (C) 1995 Spencer Kimball and Peter Mattis
  3.  * Copyright (C) 1999 Adrian Likins and Tor Lillqvist
  4.  *
  5.  * This program is free software; you can redistribute it and/or modify
  6.  * it under the terms of the GNU General Public License as published by
  7.  * the Free Software Foundation; either version 2 of the License, or
  8.  * (at your option) any later version.
  9.  *
  10.  * This program is distributed in the hope that it will be useful,
  11.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.  * GNU General Public License for more details.
  14.  *
  15.  * You should have received a copy of the GNU General Public License
  16.  * along with this program; if not, write to the Free Software
  17.  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18.  */
  19.  
  20. #include "config.h"
  21.  
  22. #include <glib.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include <ctype.h>
  26.  
  27. #include <sys/types.h>
  28. #include <sys/stat.h>
  29. #ifdef HAVE_UNISTD_H
  30. #include <unistd.h>
  31. #endif
  32. #include <fcntl.h>
  33.  
  34. #ifdef G_OS_WIN32
  35. #include <io.h>
  36. #endif
  37.  
  38. #ifndef _O_BINARY
  39. #define _O_BINARY 0
  40. #endif
  41.  
  42. #include <gtk/gtk.h>
  43.  
  44. #include "apptypes.h"
  45. #include "appenv.h"
  46. #include "brush_header.h"
  47. #include "pattern_header.h"
  48. #include "patterns.h"
  49. #include "gimpbrush.h"
  50. #include "gimpbrushpipe.h"
  51. #include "paint_core.h"
  52. #include "gimprc.h"
  53.  
  54. #include "libgimp/gimpmath.h"
  55. #include "libgimp/gimpparasiteio.h"
  56.  
  57. #include "libgimp/gimpintl.h"
  58.  
  59.  
  60. static GimpBrushClass  *parent_class;
  61.  
  62.  
  63. static GimpBrush * gimp_brush_pipe_select_brush     (PaintCore *paint_core);
  64. static gboolean    gimp_brush_pipe_want_null_motion (PaintCore *paint_core);
  65. static void        gimp_brush_pipe_destroy          (GtkObject *object);
  66.  
  67.  
  68. static GimpBrush *
  69. gimp_brush_pipe_select_brush (PaintCore *paint_core)
  70. {
  71.   GimpBrushPipe *pipe;
  72.   gint i, brushix, ix;
  73.   gdouble angle;
  74.  
  75.   g_return_val_if_fail (paint_core != NULL, NULL);
  76.   g_return_val_if_fail (GIMP_IS_BRUSH_PIPE (paint_core->brush), NULL);
  77.  
  78.   pipe = GIMP_BRUSH_PIPE (paint_core->brush);
  79.  
  80.   if (pipe->nbrushes == 1)
  81.     return GIMP_BRUSH (pipe->current);
  82.  
  83.   brushix = 0;
  84.   for (i = 0; i < pipe->dimension; i++)
  85.     {
  86.       switch (pipe->select[i])
  87.     {
  88.     case PIPE_SELECT_INCREMENTAL:
  89.       ix = (pipe->index[i] + 1) % pipe->rank[i];
  90.       break;
  91.     case PIPE_SELECT_ANGULAR:
  92.       angle = atan2 (paint_core->cury - paint_core->lasty,
  93.              paint_core->curx - paint_core->lastx);
  94.       /* Offset angle to be compatible with PSP tubes */
  95.       angle += G_PI_2;
  96.       /* Map it to the [0..2*G_PI) interval */
  97.       if (angle < 0)
  98.         angle += 2.0 * G_PI;
  99.       else if (angle > 2.0 * G_PI)
  100.         angle -= 2.0 * G_PI;
  101.       ix = RINT (angle / (2.0 * G_PI) * pipe->rank[i]);
  102.       break;
  103.     case PIPE_SELECT_RANDOM:
  104.       /* This probably isn't the right way */
  105.       ix = rand () % pipe->rank[i];
  106.       break;
  107.     case PIPE_SELECT_PRESSURE:
  108.       ix = RINT (paint_core->curpressure * (pipe->rank[i] - 1));
  109.       break;
  110.     case PIPE_SELECT_TILT_X:
  111.       ix = RINT (paint_core->curxtilt / 2.0 * pipe->rank[i]) + pipe->rank[i]/2;
  112.       break;
  113.     case PIPE_SELECT_TILT_Y:
  114.       ix = RINT (paint_core->curytilt / 2.0 * pipe->rank[i]) + pipe->rank[i]/2;
  115.       break;
  116.     case PIPE_SELECT_CONSTANT:
  117.     default:
  118.       ix = pipe->index[i];
  119.       break;
  120.     }
  121.       pipe->index[i] = CLAMP (ix, 0, pipe->rank[i]-1);
  122.       brushix += pipe->stride[i] * pipe->index[i];
  123.     }
  124.  
  125.   /* Make sure is inside bounds */
  126.   brushix = CLAMP (brushix, 0, pipe->nbrushes-1);
  127.  
  128.   pipe->current = pipe->brushes[brushix];
  129.  
  130.   return GIMP_BRUSH (pipe->current);
  131. }
  132.  
  133. static gboolean
  134. gimp_brush_pipe_want_null_motion (PaintCore *paint_core)
  135. {
  136.   GimpBrushPipe *pipe;
  137.   gint i;
  138.  
  139.   g_return_val_if_fail (paint_core != NULL, TRUE);
  140.   g_return_val_if_fail (GIMP_IS_BRUSH_PIPE (paint_core->brush), TRUE);
  141.  
  142.   pipe = GIMP_BRUSH_PIPE (paint_core->brush);
  143.  
  144.   if (pipe->nbrushes == 1)
  145.     return TRUE;
  146.  
  147.   for (i = 0; i < pipe->dimension; i++)
  148.     if (pipe->select[i] == PIPE_SELECT_ANGULAR)
  149.       return FALSE;
  150.  
  151.   return TRUE;
  152. }
  153.  
  154. static void
  155. gimp_brush_pipe_destroy (GtkObject *object)
  156. {
  157.   GimpBrushPipe *pipe;
  158.   gint i;
  159.  
  160.   g_return_if_fail (object != NULL);
  161.   g_return_if_fail (GIMP_IS_BRUSH_PIPE (object));
  162.  
  163.   pipe = GIMP_BRUSH_PIPE (object);
  164.  
  165.   g_free (pipe->rank);
  166.   g_free (pipe->stride);
  167.  
  168.   for (i = 0; i < pipe->nbrushes; i++)
  169.     if (pipe->brushes[i])
  170.       gtk_object_unref (GTK_OBJECT (pipe->brushes[i]));
  171.  
  172.   g_free (pipe->brushes);
  173.   g_free (pipe->select);
  174.   g_free (pipe->index);
  175.  
  176.   GIMP_BRUSH (pipe)->mask   = NULL;
  177.   GIMP_BRUSH (pipe)->pixmap = NULL;
  178.  
  179.   if (GTK_OBJECT_CLASS (parent_class)->destroy)
  180.     GTK_OBJECT_CLASS (parent_class)->destroy (object);
  181. }
  182.  
  183. static void
  184. gimp_brush_pipe_class_init (GimpBrushPipeClass *klass)
  185. {
  186.   GtkObjectClass *object_class;
  187.   GimpBrushClass *brush_class;
  188.  
  189.   object_class = GTK_OBJECT_CLASS (klass);
  190.   brush_class = GIMP_BRUSH_CLASS (klass);
  191.  
  192.   parent_class = gtk_type_class (GIMP_TYPE_BRUSH);
  193.  
  194.   brush_class->select_brush     = gimp_brush_pipe_select_brush;
  195.   brush_class->want_null_motion = gimp_brush_pipe_want_null_motion;
  196.  
  197.   object_class->destroy = gimp_brush_pipe_destroy;
  198. }
  199.  
  200. void
  201. gimp_brush_pipe_init (GimpBrushPipe *pipe)
  202. {
  203.   pipe->current   = NULL;
  204.   pipe->dimension = 0;
  205.   pipe->rank      = NULL;
  206.   pipe->stride    = NULL;
  207.   pipe->nbrushes  = 0;
  208.   pipe->brushes   = NULL;
  209.   pipe->select    = NULL;
  210.   pipe->index     = NULL;
  211. }
  212.  
  213. GtkType
  214. gimp_brush_pipe_get_type (void)
  215. {
  216.   static GtkType type = 0;
  217.  
  218.   if (!type)
  219.     {
  220.       GtkTypeInfo info =
  221.       {
  222.     "GimpBrushPipe",
  223.     sizeof (GimpBrushPipe),
  224.     sizeof (GimpBrushPipeClass),
  225.     (GtkClassInitFunc) gimp_brush_pipe_class_init,
  226.     (GtkObjectInitFunc) gimp_brush_pipe_init,
  227.     /* reserved_1 */ NULL,
  228.     /* reserved_2 */ NULL,
  229.     (GtkClassInitFunc) NULL
  230.       };
  231.  
  232.       type = gtk_type_unique (GIMP_TYPE_BRUSH, &info);
  233.     }
  234.  
  235.   return type;
  236. }
  237.  
  238. #include <errno.h>
  239.  
  240. GimpBrush *
  241. gimp_brush_pipe_load (gchar *filename)
  242. {
  243.   GimpBrushPipe     *pipe = NULL;
  244.   GimpPixPipeParams  params;
  245.   gint     i;
  246.   gint     num_of_brushes = 0;
  247.   gint     totalcells;
  248.   gchar   *paramstring;
  249.   GString *buffer;
  250.   gchar    c;
  251.   gint     fd;
  252.  
  253.   g_return_val_if_fail (filename != NULL, NULL);
  254.  
  255.   fd = open (filename, O_RDONLY | _O_BINARY);
  256.   if (fd == -1)
  257.     {
  258.       g_message ("Couldn't open file '%s'", filename);
  259.       return NULL;
  260.     }
  261.  
  262.   /* The file format starts with a painfully simple text header */
  263.  
  264.   /*  get the name  */
  265.   buffer = g_string_new (NULL);
  266.   while (read (fd, &c, 1) == 1 && c != '\n' && buffer->len < 1024)
  267.     g_string_append_c (buffer, c);
  268.     
  269.   if (buffer->len > 0 && buffer->len < 1024)
  270.     {
  271.       pipe = GIMP_BRUSH_PIPE (gtk_type_new (GIMP_TYPE_BRUSH_PIPE));      
  272.       GIMP_BRUSH (pipe)->name = buffer->str;
  273.     }
  274.   g_string_free (buffer, FALSE);
  275.  
  276.   if (!pipe)
  277.     {
  278.       g_message ("Couldn't read name for brush pipe from file '%s'\n", 
  279.          filename);
  280.       close (fd);
  281.       return NULL;
  282.     }
  283.  
  284.   /*  get the number of brushes  */
  285.   buffer = g_string_new (NULL);
  286.   while (read (fd, &c, 1) == 1 && c != '\n' && buffer->len < 1024)
  287.     g_string_append_c (buffer, c);
  288.  
  289.   if (buffer->len > 0 && buffer->len < 1024)
  290.     {
  291.       num_of_brushes = strtol (buffer->str, ¶mstring, 10);
  292.     }
  293.  
  294.   if (num_of_brushes < 1)
  295.     {
  296.       g_message (_("Brush pipes should have at least one brush:\n\"%s\""), 
  297.          filename);
  298.       close (fd);
  299.       gtk_object_sink (GTK_OBJECT (pipe));
  300.       g_string_free (buffer, TRUE);
  301.       return NULL;
  302.     }
  303.  
  304.   while (*paramstring && isspace (*paramstring))
  305.     paramstring++;
  306.  
  307.   if (*paramstring)
  308.     {
  309.       gimp_pixpipe_params_init (¶ms);
  310.       gimp_pixpipe_params_parse (paramstring, ¶ms);
  311.  
  312.       pipe->dimension = params.dim;
  313.       pipe->rank      = g_new0 (gint, pipe->dimension);
  314.       pipe->select    = g_new0 (PipeSelectModes, pipe->dimension);
  315.       pipe->index     = g_new0 (gint, pipe->dimension);
  316.  
  317.       /* placement is not used at all ?? */
  318.       if (params.free_placement_string)
  319.     g_free (params.placement);
  320.  
  321.       for (i = 0; i < pipe->dimension; i++)
  322.     {
  323.       pipe->rank[i] = params.rank[i];
  324.       if (strcmp (params.selection[i], "incremental") == 0)
  325.         pipe->select[i] = PIPE_SELECT_INCREMENTAL;
  326.       else if (strcmp (params.selection[i], "angular") == 0)
  327.         pipe->select[i] = PIPE_SELECT_ANGULAR;
  328.       else if (strcmp (params.selection[i], "velocity") == 0)
  329.         pipe->select[i] = PIPE_SELECT_VELOCITY;
  330.       else if (strcmp (params.selection[i], "random") == 0)
  331.         pipe->select[i] = PIPE_SELECT_RANDOM;
  332.       else if (strcmp (params.selection[i], "pressure") == 0)
  333.         pipe->select[i] = PIPE_SELECT_PRESSURE;
  334.       else if (strcmp (params.selection[i], "xtilt") == 0)
  335.         pipe->select[i] = PIPE_SELECT_TILT_X;
  336.       else if (strcmp (params.selection[i], "ytilt") == 0)
  337.         pipe->select[i] = PIPE_SELECT_TILT_Y;
  338.       else
  339.         pipe->select[i] = PIPE_SELECT_CONSTANT;
  340.       if (params.free_selection_string)
  341.         g_free (params.selection[i]);
  342.       pipe->index[i] = 0;
  343.     }
  344.     }
  345.   else
  346.     {
  347.       pipe->dimension = 1;
  348.       pipe->rank      = g_new (gint, 1);
  349.       pipe->rank[0]   = num_of_brushes;
  350.       pipe->select    = g_new (PipeSelectModes, 1);
  351.       pipe->select[0] = PIPE_SELECT_INCREMENTAL;
  352.       pipe->index     = g_new (gint, 1);
  353.       pipe->index[0]  = 0;
  354.     }
  355.  
  356.   g_string_free (buffer, TRUE);
  357.  
  358.   totalcells = 1;        /* Not all necessarily present, maybe */
  359.   for (i = 0; i < pipe->dimension; i++)
  360.     totalcells *= pipe->rank[i];
  361.   pipe->stride = g_new0 (gint, pipe->dimension);
  362.   for (i = 0; i < pipe->dimension; i++)
  363.     {
  364.       if (i == 0)
  365.     pipe->stride[i] = totalcells / pipe->rank[i];
  366.       else
  367.     pipe->stride[i] = pipe->stride[i-1] / pipe->rank[i];
  368.     }
  369.   g_assert (pipe->stride[pipe->dimension-1] == 1);
  370.  
  371.   pipe->brushes = g_new0 (GimpBrush *, num_of_brushes);
  372.  
  373.   while (pipe->nbrushes < num_of_brushes)
  374.     {
  375.       pipe->brushes[pipe->nbrushes] = gimp_brush_load_brush (fd, filename);
  376.  
  377.       if (pipe->brushes[pipe->nbrushes])
  378.     {
  379.       gtk_object_ref (GTK_OBJECT (pipe->brushes[pipe->nbrushes]));
  380.       gtk_object_sink (GTK_OBJECT (pipe->brushes[pipe->nbrushes]));
  381.       
  382.       g_free (GIMP_BRUSH (pipe->brushes[pipe->nbrushes])->name);
  383.       GIMP_BRUSH (pipe->brushes[pipe->nbrushes])->name = NULL;
  384.     }
  385.       else
  386.     {
  387.       g_message (_("Failed to load one of the brushes in the brush pipe\n\"%s\""), 
  388.                filename);
  389.       close (fd);
  390.       gtk_object_sink (GTK_OBJECT (pipe));
  391.       return NULL;
  392.     }
  393.   
  394.       pipe->nbrushes++;
  395.     }
  396.  
  397.   /* Current brush is the first one. */
  398.   pipe->current = pipe->brushes[0];
  399.  
  400.   /*  just to satisfy the code that relies on this crap  */
  401.   GIMP_BRUSH (pipe)->filename = g_strdup (filename);
  402.   GIMP_BRUSH (pipe)->spacing  = pipe->current->spacing;
  403.   GIMP_BRUSH (pipe)->x_axis   = pipe->current->x_axis;
  404.   GIMP_BRUSH (pipe)->y_axis   = pipe->current->y_axis;
  405.   GIMP_BRUSH (pipe)->mask     = pipe->current->mask;
  406.   GIMP_BRUSH (pipe)->pixmap   = pipe->current->pixmap;
  407.  
  408.   close (fd);
  409.  
  410.   return GIMP_BRUSH (pipe);
  411. }
  412.  
  413.  
  414.